# -*- coding: utf-8 -*-
from django.contrib import admin
#from djapp import views
from djapp.models import *
from DBController import *
from google.appengine.api import memcache
"""-----------------------------------------------------------------------------------"""
class CatalogSectionAdmin(admin.ModelAdmin):
    fieldsets = (
            (None, {'fields': ('section_level', 'section_name',)}),
        )
    list_display = ('section_level', 'section_name',) #no group blocks tags
    
admin.site.register(CatalogSection, CatalogSectionAdmin)    

class LevelAdmin(admin.ModelAdmin):
    fieldsets = (
            (None, {'fields': ('catalog_section_id', 'level', 'name', 'tags')}),
        )
    list_display = ('catalog_name', 'level', 'name', 'tags')
    list_display_links = ('catalog_name', 'level', 'name')
    
    def catalog_name(self, obj):
        return unicode(obj.catalog_section_id)
    
    catalog_name.short_description = 'Catalog section'
    
admin.site.register(Level, LevelAdmin)

class CategoryAdmin(admin.ModelAdmin):
    fieldsets = (
            (None, {'fields': ('header', 'wave_id', 'tags_or', 'tags_and', 'no_group')}),
        )
    list_display = ('header', 'wave_id', 'tags_or', 'tags_and', 'no_group')
    
admin.site.register(Category, CategoryAdmin)

class TagAdmin(admin.ModelAdmin):
    fieldsets = (
            (None, {'fields': ('name', 'tags')}),
        )
    list_display = ('name', 'tags')
    
admin.site.register(ExtendedTag, TagAdmin)

class TagEditorAdmin(admin.ModelAdmin):
    fieldsets = (
            (None, {'fields': ('email',)}),
        )
    list_display = ('email',)
    
admin.site.register(TagEditor, TagEditorAdmin)


def list_update(sender, instance, **kwargs):
    memcache.delete("db_controller")
    db_controller = DBController()
    if not memcache.add("has_changes", True):
        memcache.set("has_changes", True)
    if not memcache.add("db_controller", db_controller):
        memcache.set("db_controller", db_controller)
    
models.signals.post_save.connect(list_update, sender=Level)
models.signals.post_delete.connect(list_update, sender=Level)

models.signals.post_save.connect(list_update, sender=ExtendedTag)
models.signals.post_delete.connect(list_update, sender=ExtendedTag)

models.signals.post_save.connect(list_update, sender=Category)
models.signals.post_delete.connect(list_update, sender=Category)

models.signals.post_save.connect(list_update, sender=CatalogSection)
models.signals.post_delete.connect(list_update, sender=CatalogSection)